<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_donatur extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('model_donatur', 'm_don', TRUE);
    }
    
    function index() {
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        if($this->session->userdata('admin_user')->level != 1){
            redirect("admin_home","refresh");
        }
        else{
            $data = array(
                'web_title' =>  'Admin Donatur',
                'main_view' =>  'mimin/donatur/index_new',
                'list_data' =>  $this->m_don->get_all()
            );
            $this->load->view('mimin/template/template_table',$data);
        }
        
        
    }
    function add(){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            $data = array(
                'web_title' =>  'Admin Slide',
                'main_view' =>  'mimin/slide/add',
            );
            $this->load->view('mimin/template/template_upload',$data);
        }
    }
    function do_upload() {
        $headers = apache_request_headers();
        $is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest');
        if($is_ajax){
            $config['upload_path'] = './content/slides/';
            $config['allowed_types'] = 'jpg|jpeg|png';
            $config['max_width'] = '2000';
            $config['max_height'] = '488';
            $this->load->library('upload', $config);
            if (!$this->upload->do_upload()){
                echo    "<div class=\"alert alert-danger alert-dismissable\">"
                        . "<i class=\"fa fa-ban\"></i>"
                        . "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">x</button>"
                        . "<b>Alert!</b> "
                        . $this->upload->display_errors()
                        . "</div>";
            }
            else{
                date_default_timezone_set("Asia/Jakarta");
                $curdate = date("Y-m-d h:i:s");
                $upload_image = $this->upload->data();
                
                //check ratio
                if((int)$upload_image['image_width']/(int)$upload_image['image_height']!== 2000/488){
                                                                               
                    echo    "<div class=\"alert alert-danger alert-dismissable\">"
                        . "<i class=\"fa fa-ban\"></i>"
                        . "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">x</button>"
                        . "<b>Alert!</b> "
                        . "Ratio image (width : height) must be 2000 : 488, ".$upload_image['image_width']/$upload_image['image_height']
                        . "</div>";
                    exit();
                }
                //create thumbnail
                $config['image_library']    = 'gd2';
                $config['source_image']     = 'content/slides/'.$upload_image['file_name'];
                $config['create_thumb']     = TRUE;
                $config['maintain_ratio']   = TRUE;
                $config['width']            = 500;
                $config['height']           = 122;
                $config['new_image']        = 'content/slides/thumb/'.$upload_image['file_name'];
                $config['thumb_marker']     = '';

                $this->load->library('image_lib', $config);
                $this->image_lib->resize();

                $isi = array(
                    'createdby'     =>  $this->session->userdata('admin_user')->id_admin,
                    'datecreated'   =>  $curdate,
                    'path'          =>  'content/slides/'.$upload_image['file_name'],
                    'thumbnail'     =>  'content/slides/thumb/'.$upload_image['file_name'].''
                );
                if($this->m_slide->save($isi)){
                    echo "<div class=\"alert alert-success alert-dismissable\">"
                        . "<i class=\"fa fa-check\"></i>"
                        . "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">x</button>"
                        . "<b>Alert!</b> "
                        . "New Slide image has been uploaded."
                        . "</div>";
                }
                else{
                    echo "<div class=\"alert alert-danger alert-dismissable\">"
                        . "<i class=\"fa fa-ban\"></i>"
                        . "<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">x</button>"
                        . "<b>Alert!</b> "
                        . "Failed to upload image."
                        . "</div>";
                }
            }
        }
        else{
            die("Cannot access directly");
        }
    }
    function delete(){
        $headers = apache_request_headers();
        $is_ajax = (isset($headers['X-Requested-With']) && $headers['X-Requested-With'] == 'XMLHttpRequest');
        if($is_ajax){
            if(!$this->session->userdata('admin_user')){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "You need to re login!",
                );
                echo json_encode($output);exit();
            }
            if($this->session->userdata('admin_user')->level != 1){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "You have no rights to run this function!",
                );
                echo json_encode($output);exit();
            }
            
            $this->form_validation->set_rules('id_slide','id','required');
            if($this->form_validation->run() == FALSE){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Missing ID!",
                );
                echo json_encode($output);exit();
            }
            else{
                $count = $this->m_slide->select_by_id($this->input->post("id_slide"),"count");
                if($count > 0){
                    $status = $this->m_slide->delete($this->input->post("id_slide"));
                    if($status){
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Data successfully deleted, page will be refreshed in 3 seconds!",
                        );
                        echo json_encode($output);exit();
                    }
                    else{
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Data deletion is failed!",
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Data already deleted, please refresh the page!",
                    );
                    echo json_encode($output);exit();
                }
            }
        }
        else{
            die("cannot access directly");
        }
        
    }
    function detail($id=""){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id==""){
            redirect("admin_donatur","refresh");
        }
        else{
            //SET TIME ZONE
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date("Y-m-d h:i:s");
            
            $count = $this->m_event->select_by_id($id,"count");
     
            if($count > 0){
                $this->form_validation->set_rules('judul','Judul','required');
                $this->form_validation->set_rules('content','Isi Konten','required');
                if($this->form_validation->run() == FALSE){
                    $data = array(
                        "main_view"     =>  "mimin/event/detail",
                        "web_title"     =>  "Data Event Detail",
                        "list_data"     =>  $this->m_event->select_by_id($id)
                    );            
                    $this->load->view("mimin/template/template_editor",$data);
                }
                else{
                    $config['upload_path'] = './content/media/events/';
                    $config['allowed_types'] = 'jpg|jpeg|png';
                    $this->load->library('upload', $config);
                    
                    //IF THE ADMIN Do Not Choose any image to upload
                    if (!$this->upload->do_upload()){
                        //echo"sini".$this->upload->display_errors();exit();
                        $isi_update = array(
                            "judul"         =>  $this->input->post("judul"),
                            "place"         =>  $this->input->post("place"),
                            "content"       =>  $this->input->post("content"),
                            "datemodified"  =>  $curdate,
                            "modifiedby"    =>  $this->session->userdata('admin_user')->id_admin,
                            "eventdate"     =>  $this->input->post("tanggal_event"),
                        );
                        $status_update = $this->m_event->update($id,$isi_update);
                        if($status_update == false){
                            echo"false";
                        }
                        else{
                            $data = array(
                                "main_view"         =>  "mimin/event/detail",
                                "web_title"         =>  "Successfull Update Event Data",
                                "success_status"    =>  1,
                                "list_data"         =>  $this->m_event->select_by_id($id)
                            );
                            $this->load->view("mimin/template/template_editor",$data);
                        }
                        $data = array(
                            "main_view"     =>  "mimin/event/add_new",
                            "web_title"     =>  "Add New Event Data",
                            "error"         =>  $this->upload->display_errors()
                        );
                        //$this->load->view("mimin/template/template_editor",$data);
                    }
                    else{
                        //echo"sini";exit();
                        
                        $upload_image = $this->upload->data();
                        $isi_update = array(
                            'judul'         =>  $this->input->post('judul'),
                            'content'       =>  $this->input->post('content'),
                            'datemodified'  =>  $curdate,
                            'place'         =>  $this->input->post('place'),
                            "modifiedby"    =>  $this->session->userdata('admin_user')->id_admin,
                            'image'         =>  'content/media/events/'.$upload_image['file_name'],
                            "eventdate"     =>  $this->input->post("tanggal_event"),
                        );
                        
                        $status_update = $this->m_event->update($id,$isi_update);
                        if($status_update){
                            $data = array(
                                "main_view"         =>  "mimin/event/detail",
                                "web_title"         =>  "Successfull Add new Event Data",
                                "success_status"    =>  1,
                                "list_data"         =>  $this->m_event->select_by_id($id)
                            );
                            $this->load->view("mimin/template/template_editor",$data);
                        }
                        else{
                            echo"Cannot update data";
                        }
                    }
                }
            }
            else{
                redirect("admin_donatur","refresh");
            }
        }
    }
}

/* End of file admin_donatur.php */
/* Location: ./application/controllers/admin_donatur.php */